package org.example.demo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.demo.entity.User;
import org.example.demo.entity.UserStatus;
import org.example.demo.mapper.UserMapper;
import org.example.demo.mapper.UserStatusMapper;
import org.example.demo.service.UserService;
import org.example.demo.vo.UserStatusCntVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserStatusMapper userStatusMapper;


    private static final String userStatusCategory[] = {"群众", "正式党员", "预备党员", "入党积极分子", "发展对象"};
    @Override
    public User checkLogin(String userAccount, String pwd) {
       return userMapper.checkPwdWithAccount(userAccount,pwd);
    }


    /**
     * 通过用户id获取用户状态
     * @param id
     * @return
     */
    public List<UserStatus> listStatus(Integer id) {

        List<UserStatus> userStatusList = userStatusMapper.getByUserId(id);

        return userStatusList;
    }

    /**
     * 插入用户状态数据
     * @param user
     */
    public void insertUserStatus(User user) {
        Long user_id = user.getId();
        String status = user.getPoliticalStatus();

        UserStatus userStatus = UserStatus.builder()
                .userId(user_id)
                .status(status)
                .updateTime(LocalDateTime.now())
                .build();

        userStatusMapper.insert(userStatus);
    }

    /**
     * 获取用户状态数量
     * @return
     */
    public List<UserStatusCntVO> getUserStatusCnt() {

        List<UserStatusCntVO> userStatusCntVOList = userMapper.getUserStatusCnt();

        // 看看List中有没有这个类型
        // 如果没有，就加入数量0
        for (String status : userStatusCategory) {
            boolean ok = false;
            for (UserStatusCntVO userStatusCntVO : userStatusCntVOList) {
                if (userStatusCntVO.getStatus().equals(status)) {
                    ok = true;
                    break;
                }
            }

            if (!ok) userStatusCntVOList.add(new UserStatusCntVO(status, 0));

        }

        return userStatusCntVOList;
    }
}
